package com.xyzq.simpson.apm.web.controller;

import com.xyzq.simpson.apm.web.utils.DurationUtil;
import com.xyzq.simpson.apm.web.utils.ResponseEntity;
import com.xyzq.simpson.hibbert.storage.es.brief.AlarmBrief;
import com.xyzq.simpson.hibbert.storage.es.dao.IAlarmQueryDAO;
import com.xyzq.simpson.hibbert.storage.es.manager.EsQueryManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/apm/api/alarm")
public class AlarmController {
    protected Logger logger = LoggerFactory.getLogger(AlarmController.class);

    @RequestMapping(value = "/alarmList", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity alarmList(@RequestBody Map<String, Object> json) throws Exception {
        IAlarmQueryDAO alarmQueryEsDAO = EsQueryManager.instance().getService(IAlarmQueryDAO.class);
        long startTimeStamp = DurationUtil.getStartTimeStamp(json);
        long endTimeStamp = DurationUtil.getEndTimeStamp(json);
        Map<String, Object> paging = (Map<String, Object>)json.get("paging");
        int page = Integer.valueOf(""+paging.get("pageNum"));
        int size = Integer.valueOf(""+paging.get("pageSize"));
        String ruleName = json.get("ruleName") == null || "ALL".equalsIgnoreCase(""+json.get("ruleName")) ? "" : String.valueOf(json.get("ruleName"));
        String project = json.get("serviceId") == null || "ALL".equalsIgnoreCase(""+json.get("serviceId")) ? "" : String.valueOf(json.get("serviceId"));
        String ip = json.get("serviceInstanceId") == null || "ALL".equalsIgnoreCase(""+json.get("serviceInstanceId")) ? "" : String.valueOf(json.get("serviceInstanceId"));
        String match = json.get("match") == null ? "" : String.valueOf(json.get("match"));
        int from = (page - 1) * size;
        AlarmBrief alarmBrief = alarmQueryEsDAO.queryAlarms(startTimeStamp, endTimeStamp, project, ip, ruleName, match, from, size);
        return new ResponseEntity().success(alarmBrief);
    }
}